package com.hkts.management.message.msg.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.utils.ResultUtil;
import com.hkts.management.message.msg.entity.MsgTemplate;
import com.hkts.management.message.msg.service.IMsgTemplateService;
import com.hkts.management.message.utils.CustomPageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zy
 * @since 2021-05-10
 */
@RestController
@RequestMapping("/message/msgTemplate")
public class MsgTemplateController {
    private final Logger logger = LoggerFactory.getLogger(MsgTemplateController.class);

    @Autowired
    public IMsgTemplateService iMsgTemplateService;

    /**
     * 分页查询数据
     *
     * @param page        分页信息
     * @param msgTemplate 查询条件
     * @return
     */
    @ResponseBody
    @PostMapping("/getMsgTemplatePageList")
    public Result<Object> getHontUserList(Page<MsgTemplate> page, MsgTemplate msgTemplate) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iMsgTemplateService.selectPage(page, msgTemplate)));
    }

    /**
     * 保存和修改公用的
     *
     * @param msgTemplate 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/msgTemplateSave")
    public Result<Object> msgTemplateSave(MsgTemplate msgTemplate) {
        iMsgTemplateService.saveOrUpdate(msgTemplate);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/msgTemplateGetById")
    public Result<Object> msgTemplateGetById(String id) {
        MsgTemplate byId = iMsgTemplateService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/msgTemplateDelete")
    public Result<Object> hontUserDelete(String id) {
        iMsgTemplateService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/msgTemplateBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iMsgTemplateService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/sendSubscript")
    public Result<Object> sendSubscript(String userId) {
        iMsgTemplateService.sendSubscript(userId);
        return ResultUtil.success();
    }

}
